package com.pandora.credit.service.impl;

import com.pandora.common.util.Constants;
import com.pandora.credit.dao.AnnouncementDao;
import com.pandora.credit.dto.request.AnnouncementRequest;
import com.pandora.credit.dto.response.AnnouncementResponse;
import com.pandora.credit.dto.response.LoanResponseNextLong;
import com.pandora.credit.dto.response.LoanResponseNextStr;
import com.pandora.credit.entity.Announcement;
import com.pandora.credit.exception.InvalidResponseException;
import com.pandora.credit.mongo.entity.MongoAnnouncement;
import com.pandora.credit.mongo.service.MongoAnnouncementService;
import com.pandora.credit.service.AnnouncementService;
import com.pandora.credit.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author developer
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementDao announcementDao;
    @Autowired
    private MongoAnnouncementService mongoAnnouncementService;
    @Autowired
    private CommonService commonService;

    @Override
    public List<Announcement> queryByMidValid(long mid, int valid) {
        return announcementDao.queryByMidValid(mid, valid);
    }

    @Override
    public List<Announcement> queryByValidltId(long id, int valid) {
        return announcementDao.queryByValidltId(id, valid);
    }

    @Override
    public AnnouncementResponse doGetDetailAnnouncement(long mId) {
        List<Announcement> annMys = queryByMidValid(mId, Constants.ITEM_STATUS_VALID);
        MongoAnnouncement ann = mongoAnnouncementService.queryById(mId);
        if(annMys == null || annMys.size() <= 0 || ann == null){
            throw new InvalidResponseException();
        }
        Announcement annMy = annMys.get(0);
        return new AnnouncementResponse(annMy.getId(),
                annMy.getTitle(),
                annMy.getSubtitle(),
                annMy.getDestUrl(),
                annMy.getCreateTime(),
                annMy.getUpdateTime(),
                annMy.getMid(),
                annMy.getValid(),
                ann.getContent());
    }

    @Override
    public LoanResponseNextLong doGetAnnouncement(String ua, AnnouncementRequest req) {
        int length = req.getLength();
        long nextId = req.getNextId()!=null?req.getNextId():-1;
        List<Announcement> qry = queryByValidltId(nextId, Constants.ITEM_STATUS_VALID);

        if(length <= 0){
            length = Constants.DEFAULT_PAGE_LENGTH;
        }
        int need = qry.size() >= length?length:qry.size();
        List<Announcement> items = new LinkedList<>();
        for(int i = 0; i < need; i++){
            items.add(qry.get(i));
        }
        Long nextIdStr = need>0?qry.get(need-1).getId():null;

        int k = commonService.getAppStatusLimit(ua);
        if(k == Constants.VERSION_STATUS_CHECKING){
            items = new LinkedList<>();
        }
        return new LoanResponseNextLong(
                Constants.RETURN_TYPE_ANNOUNCEMENT,
                items,
                nextIdStr
        );
    }
}
